package com.example.service.webhook.business.alipay.repository;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.service.webhook.business.alipay.entity.AlipayTradeCategoryEntity;
import com.example.service.webhook.business.alipay.entity.AlipayTradeRecordEntity;
import com.example.service.webhook.business.alipay.entity.AlipayWealthEntity;
import com.example.service.webhook.business.alipay.entity.AlipayYuebaoTradeInfoEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
/**
 * 支付宝账单周期消费分类信息
 * 这里用的是jdbctemplate，DAO层可以根据自己的业务来选择
 * ClassName: AlipayUserInfoRepository    
 * date: 2016年7月19日 下午6:29:23   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Repository
public class AlipayTradeCategoryRepository extends BaseDAO<AlipayTradeCategoryEntity>{
	
	@Resource(name="templateAlipay")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
    
    public void saveAlipayTradeCategory(AlipayTradeCategoryEntity entity) throws Exception  {
    	entity.setCreateTime(DateUtil.getCurrentDate());
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.add(entity);
    }
    
    
    public void updateAlipayTradeCategory(AlipayTradeCategoryEntity entity) throws Exception  {
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.modify(entity);
    }
    
   
    public AlipayTradeCategoryEntity getAlipayTradeCategory(String userId,String mappingId,String billCycle) throws Exception {
        String sql = "";
        String tableName = "t_alipay_tradecategory";
        sql = "select * from " + tableName + " where userId=? and mappingId=? and billCycle=?";
        AlipayTradeCategoryEntity pojo = null;
        try{
         pojo = (AlipayTradeCategoryEntity) queryOne(sql, new Object[] {userId,mappingId,billCycle}, new AlipayTradeCategoryEntity());	
        }catch(EmptyResultDataAccessException e){
        	return null;
        }
        
        return pojo;
    }
    
    public void deleteAlipayTradeCategory(String userId,String mappingId){
 	   String tableName = "t_alipay_tradecategory";
 	        StringBuilder sql = new StringBuilder("DELETE FROM ").append(tableName).append(" WHERE ").
 	                append("userId='").append(userId).append("'").
 	                append("and mappingId='").append(mappingId).append("'");

 	         this.getJdbcTemplate().update(sql.toString());
    }
    
    
}
